package org.cid54.android.jshopper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import org.cid54.android.jshopper.dao.DaoCallBackMethod;
import org.cid54.android.jshopper.dao.ShopItemDao;
import org.cid54.android.jshopper.dao.ShoppingListDao;
import org.cid54.android.jshopper.dao.impl.ShopItemDaoImpl;
import org.cid54.android.jshopper.dao.impl.ShoppingListDaoImpl;
import org.cid54.android.jshopper.model.ShopItem;
import org.cid54.android.jshopper.model.ShoppingList;

/**
 *
 * @author fgo
 */
public class ShoppingListDetail extends Activity {

    public static final String SHOPPINGLIST_PARAM_NAME = "shoppingListItem";
    private ShopItemDao shopItemDao = new ShopItemDaoImpl();
    private ShoppingListDao shoppingListDao = new ShoppingListDaoImpl();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.shoppinglist_menu, menu);
        return true;
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        ShoppingList shoppingList = (ShoppingList) this.getIntent().getExtras().get(SHOPPINGLIST_PARAM_NAME);

        this.shopItemDao.getShopItemsByShoppingList(shoppingList, new DaoCallBackMethod<List<ShopItem>>() {

            public void execute(List<ShopItem> t) {

                ShoppingListDetail.this.setListView(t);

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.shoppinglist_menu_add_item:

                Intent newShopItem = new Intent(this.getApplicationContext(), NewShopItem.class);

                ShoppingList shoppingList = (ShoppingList) this.getIntent().getExtras().get(SHOPPINGLIST_PARAM_NAME);

                newShopItem.putExtra(NewShopItem.SHOPPINGLIST_PARAM_NAME, shoppingList);

                startActivity(newShopItem);

                return true;
            case R.id.shoppinglist_menu_delete_list:
                this.deleteList();
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }

    }

    private void deleteList() {
        DaoCallBackMethod<Void> callBack = new DaoCallBackMethod<Void>() {

            public void execute(Void t) {
                finish();
            }
        };

        this.shoppingListDao.deleteShoppingList((ShoppingList) this.getIntent().getExtras().get(SHOPPINGLIST_PARAM_NAME), callBack);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.shoppinglist_detail);

        ShoppingList shoppingList = (ShoppingList) this.getIntent().getExtras().get(SHOPPINGLIST_PARAM_NAME);

        TextView textView = (TextView) findViewById(R.id.shoppingListName);

        textView.setText(shoppingList.getName());

        Button b = (Button) findViewById(R.id.backButton);
        b.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                finish();
            }
        });

        this.shopItemDao.getShopItemsByShoppingList(shoppingList, new DaoCallBackMethod<List<ShopItem>>() {

            public void execute(List<ShopItem> t) {

                ShoppingListDetail.this.setListView(t);

            }
        });

    }

    private void setListView(List<ShopItem> shopItems) {
        ListView listView = (ListView) findViewById(R.id.shoppingListItems);
        listView.setAdapter(new ShoppingItemAdapter(this, R.layout.shop_item_listview, shopItems));

        listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShopItem item = (ShopItem) parent.getAdapter().getItem(position);

                shopItemDao.update(item, new DaoCallBackMethod<Void>() {

                    public void execute(Void t) {
                        onShopItemUpdateComplete();
                    }
                });
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                ShopItem item = (ShopItem) parent.getAdapter().getItem(position);

                shopItemDao.delete(item, new DaoCallBackMethod<Void>() {

                    public void execute(Void t) {
                        onShopItemUpdateComplete();
                    }
                });

                return true;

            }
        });
    }

    private void onShopItemUpdateComplete() {

        onPostResume();

    }

    class ShoppingItemAdapter extends ArrayAdapter<ShopItem> {

        final NumberFormat formatter = new DecimalFormat("00");

        public ShoppingItemAdapter(Context context, int textViewResourceId, List<ShopItem> objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View shopItemView = null;

            if (convertView == null) {
                LayoutInflater vi = getLayoutInflater();
                shopItemView = vi.inflate(R.layout.shop_item_listview, parent, false);
            } else {
                shopItemView = convertView;
            }

            ShopItem item = this.getItem(position);

            TextView shopItemName = (TextView) shopItemView.findViewById(R.id.shopItemName);

            shopItemName.setText(item.getName());

            TextView shopItemAmount = (TextView) shopItemView.findViewById(R.id.shopItemAmount);

            shopItemAmount.setText("x" + formatter.format(item.getAmount()));

            if (item.isBought()) {
                shopItemName.setTextColor(Color.GREEN);
                shopItemAmount.setTextColor(Color.GREEN);
            } else {
                shopItemName.setTextColor(Color.LTGRAY);
                shopItemAmount.setTextColor(Color.LTGRAY);
            }

            return shopItemView;
        }
    }
}
